$(() => {obj.init()});
let obj = {
    dom: {

    },
    params: {
        code: null
    },
    init: function () {

        this.listener();
    },
    listener: function () {
        //查看工资单
        $('#bt_view').click(() => {
            this.funObj().getCode();
        });
    },
    funObj: function () {
        return {
            getCode: () => {
                $('#salary_view').hide();
                xhExtra.setBtnNormal($('#bt_view'));
                xhExtra.ajaxRequest('hrm/salary/getCode', {}, {check: true}, (data) => {
                    if (data.code === 201 && this.params.code) {
                        this.funObj().viewSalary();
                    }
                    else {
                        window.top.swal({
                            title: '手机验证码',
                            input: 'text',
                            inputPlaceholder: '请输入验证码',
                            showCancelButton: true,
                            confirmButtonText: '确认',
                            cancelButtonText: '取消',
                            showLoaderOnConfirm: true,
                            allowOutsideClick: false,
                            preConfirm: (code) => {
                                return new Promise((resolve, reject) => {
                                    if(!code){
                                        reject('验证码不能为空');
                                    } else {
                                        this.params.code = code;
                                        this.funObj().viewSalary();
                                        resolve();
                                    }
                                })
                            }

                        })
                    }
                });
            },
            viewSalary: () => {
                xhExtra.ajaxRequest('hrm/salary/getSalary', {year: $('#year').val(), code: this.params.code}, {check: false}, (result) => {
                    if (xhExtra.checkError(result)) {
                        this.params.code = null;
                        return false;
                    }
                    let payrollBody = '',
                        reimburseBody = '',
                        totalBody = '';
                    $.each(result.payroll, (index, row) => {
                        payrollBody += `<tr>
                                 <td>${row.month}月</td>
                                 <td>${row.basicWage}</td>
                                 <td>${row.phoneWage}</td>
                                 <td>${row.totalSub}</td>
                                 <td>${row.overtimeSub}</td>
                                 <td>${row.kpiSub}</td>
                                 <td>${row.otherSub}</td>
                                 <td>${row.phoneSub}</td>
                                 <td>${row.deductWage}</td>
                                 <td>${row.totalPay}</td>
                                 <td>${row.otherHold}</td>
                                 <td>${row.socialHold}</td>
                                 <td>${row.cpfHold}</td>
                                 <td>${row.taxWage}</td>
                                 <td>${row.taxHold}</td>
                                 <td>${row.totalHold}</td>
                                 <td>${row.finalPay}</td>
                             </tr>`;
                    });
                    $.each(result.reimburse, (index, row) => {
                        reimburseBody += `<tr>
                                 <td>${row.month}月</td>
                                 <td>${row.officeIn}</td>
                                 <td>${row.dailyIn}</td>
                                 <td>${row.bookIn}</td>
                                 <td>${row.entertainIn}</td>
                                 <td>${row.foodIn}</td>
                                 <td>${row.otherIn}</td>
                                 <td>${row.totalIn}</td>
                                 <td>${row.trafficOut}</td>
                                 <td>${row.layoverOut}</td>
                                 <td>${row.lodgeOut}</td>
                                 <td>${row.gasOut}</td>
                                 <td>${row.otherOut}</td>
                                 <td>${row.totalOut}</td>
                                 <td>${row.totalPay}</td>
                             </tr>`;
                    });
                    totalBody += `<p><b>薪金总额：</b>${result.total.finalPay}</p>`;
                    totalBody += `<p><b>报销总额：</b>${result.total.totalPay}</p>`;
                    totalBody += `<p><b>驻外补贴：</b>${result.total.layoverOut}</p>`;
                    totalBody += `<p><b>总计收入：</b>${result.total.total}</p>`;
                    $('#payroll_title').text(result.year + '-工资单');
                    $('#payroll_body').html(payrollBody);
                    $('#reimburse_title').text(result.year + '-报销单');
                    $('#reimburse_body').html(reimburseBody);
                    $('#total_body').html(totalBody);
                    $('#salary_view').fadeIn();
                    $('body').css('overflow-y', 'auto');
                });
            }
        }
    }
};